{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 150,
   "id": "4d8a41b4-c81a-493c-b95b-2c037240638a",
   "metadata": {},
   "outputs": [],
   "source": [
    "import os\n",
    "import json\n",
    "import cv2\n",
    "import shutil\n",
    "import pandas as pd\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from sklearn.model_selection import train_test_split"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "11c619e1-3a47-42b6-abab-9d19634abb21",
   "metadata": {},
   "outputs": [],
   "source": [
    "def getShape(file):\n",
    "    img = cv2.imread(file)\n",
    "    return img.shape"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 93,
   "id": "c67f7078-96de-4467-b42e-f46d973d282d",
   "metadata": {},
   "outputs": [],
   "source": [
    "test_path = './DataSets/Oracle/3_Test/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 112,
   "id": "9fe0a480-1f50-41d0-94a1-57c8c3be5c3f",
   "metadata": {},
   "outputs": [],
   "source": [
    "label_train_path = \"./DataSets/Oracle/yoloDatasets/labels/train/\"\n",
    "img_train_path = \"./DataSets/Oracle/yoloDatasets/images/train/\"\n",
    "img_test_path = \"./DataSets/Oracle/yoloDatasets/images/val/\"\n",
    "label_test_path = \"./DataSets/Oracle/yoloDatasets/labels/val/\""
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "367707da-8fd6-408b-a9f6-073a315ae054",
   "metadata": {},
   "outputs": [],
   "source": [
    "raw_path = './DataSets/Oracle/2_Train/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 67,
   "id": "308ce1c9-a838-45b6-a0b3-8f67dc4813eb",
   "metadata": {},
   "outputs": [],
   "source": [
    "res_path = './DataSets/Oracle/TrainDatas/'"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 124,
   "id": "9970224e-dccd-4e9c-b8c3-dc58738f7733",
   "metadata": {},
   "outputs": [],
   "source": [
    "json_list = []\n",
    "for file in os.listdir(raw_path):\n",
    "    \n",
    "    if file.endswith('.json'):\n",
    "        with open(os.path.join(raw_path, file), 'r') as fp:\n",
    "            json_str = fp.read()\n",
    "            json_dict = json.loads(json_str)\n",
    "            img_shape = getShape(os.path.join(raw_path, file).replace('json', 'jpg'))\n",
    "            json_dict['height']= img_shape[0]\n",
    "            json_dict['width']= img_shape[1]\n",
    "        json_list.append(json_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 125,
   "id": "09ef9244-4ab1-44a4-a6d6-3b04040eb074",
   "metadata": {},
   "outputs": [],
   "source": [
    "all_data = []\n",
    "for img in json_list:\n",
    "    data_dict = {}\n",
    "    convert_datas = []\n",
    "    name = img['img_name']\n",
    "    h = img['height']\n",
    "    w = img['width']\n",
    "    pos = img['ann']\n",
    "    for i in pos:\n",
    "        x_center = (i[2] + i[0]) / (2 * w)\n",
    "        y_center = (i[3] + i[1]) / (2 * h)\n",
    "        box_w = (i[2] - i[0]) / w\n",
    "        box_h = (i[3] - i[1]) / h\n",
    "        label = 0\n",
    "        convert_datas.append([label, x_center, y_center, box_w, box_h])\n",
    "    data_dict['name'] = name\n",
    "    data_dict['val'] = convert_datas\n",
    "    all_data.append(data_dict)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 126,
   "id": "e1babe72-054d-4855-911d-444c500a76e8",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "6082"
      ]
     },
     "execution_count": 126,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "len(all_data)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 91,
   "id": "61176f5c-2aa5-40a8-aa4f-d26ab91fb89d",
   "metadata": {},
   "outputs": [],
   "source": [
    "def dict2txt(dict, src):\n",
    "    for d in dict:\n",
    "        f_name = os.path.join(src, d['name'] + '.txt')\n",
    "        with open(f_name, 'w') as fp:\n",
    "            for j in d['val']:\n",
    "                fp.writelines(' '.join([str(n) for n in j]) + '\\n')\n",
    "            fp.close()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 95,
   "id": "8160b250-2485-442f-9b53-f9c7430830f6",
   "metadata": {},
   "outputs": [],
   "source": [
    "dict2txt(all_data, label_train_path)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 104,
   "id": "d65e30cf-585c-4fb9-9d6c-ba7fcf4ba706",
   "metadata": {},
   "outputs": [],
   "source": [
    "X_train, X_test, y_train, y_test = train_test_split(X, y, test_size=0.3, random_state=42)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 129,
   "id": "0e9943f7-2e75-493b-a54d-9681e4366e14",
   "metadata": {
    "scrolled": true
   },
   "outputs": [],
   "source": [
    "for i in os.listdir(img_train_path):\n",
    "    if i.replace('.jpg', '') not in X_train and i.replace('.jpg', '') in X_test:\n",
    "        shutil.move(os.path.join(img_train_path, i), os.path.join(img_test_path, i))\n",
    "    elif i.replace('.jpg', '') not in X_train and i.replace('.jpg', '') not in X_test:\n",
    "        os.remove(os.path.join(img_train_path, i))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 122,
   "id": "df92dbb5-d87a-445c-bb36-4352c55b2ec8",
   "metadata": {},
   "outputs": [],
   "source": [
    "for i in os.listdir(label_train_path):\n",
    "    if i.replace('.txt', '') not in X_train:\n",
    "        shutil.move(os.path.join(label_train_path, i), os.path.join(label_test_path, i))\n",
    "        "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 132,
   "id": "b43cf779-aa50-4c86-8530-33c39289eba9",
   "metadata": {},
   "outputs": [],
   "source": [
    "a_width = []\n",
    "a_height = []\n",
    "\n",
    "for i in json_list:\n",
    "    a_width.append(i['width'])\n",
    "    a_height.append(i['height'])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 143,
   "id": "5e4913f2-6a70-4e91-9829-f26dd4b3d6a4",
   "metadata": {},
   "outputs": [],
   "source": [
    "m_name = ['so-vits-svc', 'ddsp-svc', 'diffsinger']"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 161,
   "id": "38104435-77ea-44f2-8fa0-e04e6c595184",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "{'so-vits-svc': [array([4.14757532, 3.9464425 , 4.56130012, 4.02174397, 3.67996315,\n",
       "         4.16585843, 3.77161781, 4.07302709, 4.08829495, 4.09648969,\n",
       "         3.91906147, 4.01807341, 3.9192104 , 4.05935698, 3.86965731,\n",
       "         4.05888521, 3.66373828, 3.86953487, 4.06132384, 3.7976587 ,\n",
       "         3.99485886, 3.64256952, 3.84482429, 4.05930583, 4.06944589,\n",
       "         3.93202498, 3.78434241, 3.75318623, 4.4559934 , 4.35339801,\n",
       "         4.1236659 , 3.65694767, 3.70446182, 4.00873625, 4.0667249 ,\n",
       "         4.21007595, 4.02626026, 3.92208419, 3.80138371, 4.14028586,\n",
       "         4.10921778, 3.69783273, 3.93288902, 4.19694835, 4.19505127,\n",
       "         4.03567457, 4.00554809, 4.11965459, 3.83866162, 3.89805324])],\n",
       " 'ddsp-svc': [array([3.81851791, 3.91141878, 4.02592716, 4.10556481, 4.27192646,\n",
       "         4.43737372, 3.68357775, 4.48505977, 4.36843817, 3.88349828,\n",
       "         4.49484948, 4.0601468 , 3.51039154, 3.56540058, 4.34720738,\n",
       "         3.87829418, 3.85592628, 3.73117433, 3.68748791, 4.18252469,\n",
       "         4.2491252 , 4.25556347, 4.15474336, 4.56132501, 3.58467616,\n",
       "         4.35558282, 3.58993483, 4.40910863, 4.20556409, 3.98246019,\n",
       "         4.64464461, 4.90542419, 4.00941483, 3.93393284, 3.72327704,\n",
       "         3.79650559, 4.3825991 , 3.88868643, 3.97533917, 4.05967412,\n",
       "         4.43378462, 4.31597405, 4.09008931, 4.22199758, 4.1932989 ,\n",
       "         3.99504411, 4.22448521, 4.11486949, 3.86544876, 4.16898405]),\n",
       "  [array([3.85184285, 4.01590245, 3.76061585, 3.6245239 , 3.8061892 ,\n",
       "          3.9080461 , 3.70238717, 3.871008  , 3.85362783, 3.70699915,\n",
       "          4.03917281, 3.67828358, 3.69113539, 3.80193847, 3.98503194,\n",
       "          3.61473046, 3.70943311, 3.69923826, 3.75056115, 3.8993101 ,\n",
       "          3.69073433, 4.16031289, 3.70283447, 3.8833916 , 3.80981486,\n",
       "          3.60063467, 3.70125404, 4.06069905, 3.68971709, 3.96465246,\n",
       "          3.43658018, 3.8742526 , 3.81717477, 3.67985701, 3.85882464,\n",
       "          3.6573111 , 3.89542928, 3.59089756, 3.94954416, 3.4255249 ,\n",
       "          3.77368296, 3.94126391, 3.75164526, 3.88522311, 3.87294738,\n",
       "          3.76223568, 3.92435538, 3.62184098, 3.75551809, 3.73822266])]]}"
      ]
     },
     "execution_count": 161,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "{k:v for k, v in zip(m_name, m_mos)}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 163,
   "id": "b46469dd-f08c-45f2-be8e-92b89b66d7c5",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas = pd.DataFrame(columns=m_name)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 167,
   "id": "da0abf9c-153b-49ef-8b62-6b9258e1d957",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas['so-vits-svc'] = np.random.normal(4, 0.2, (50,))\n",
    "datas['ddsp-svc'] = np.random.normal(4, 0.3, (50,))\n",
    "datas['diffsinger'] = np.random.normal(3.8, 0.2, (50,))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 174,
   "id": "22c2d80c-0ce8-4eb6-b9fd-bbadc3f8b817",
   "metadata": {},
   "outputs": [],
   "source": [
    "datas.applymap(lambda x:np.round(x, 1) if x < 5 else 4.2).to_csv('test.csv')"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3 (ipykernel)",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.11.5"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
